#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 该文件主要用于各种请求方法
import requests
import json

from commons.logger import log



class Req:

    def get(self, url, params=None, headers=None, stream=None, timeout=None, res_format=None):
        """
        get请求方法
        res_format: 对返回的接口结果格式指定(目前是text、原生和json后期需要可以加判断)
        """
        try:

            r = requests.get(url, params=params, headers=headers, stream=stream, timeout=timeout)
            r.encoding = 'utf-8'
            if res_format and res_format == "text":  # 用于接口返回的是html的请求,返回text格式信息
                res_data = r.text
            elif res_format and res_format == "raw":  # 用于接口返回的未做处理原生格式
                res_data = r
            else:
                res_data = r.json()  # 用于接口返回的是json的请求,返回json格式信息
            log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (res_data, url))
            return res_data
        except BaseException as e:
            log.info("请求异常！请求失败url: %s,错误信息: %s" % (url, str(e)))

    def post(self, url, params=None, data=None, files=None, headers=None, stream=None, res_format=None, timeout=None):
        """
        post请求方法,请求data未转为json,将返回结果转成json格式
        res_format: 对返回的接口结果格式指定(目前是text、原生和json后期需要可以加判断)
        """
        try:
            r = requests.post(url, params=params, data=data, files=files, headers=headers, stream=stream,
                              timeout=timeout)
            r.encoding = 'utf-8'
            if res_format and res_format == "text":  # 用于接口返回的是html的请求,返回text格式信息
                res_data = r.text
            elif res_format and res_format == "raw":  # 用于接口返回的未做处理原生格式
                res_data = r
            else:
                res_data = r.json()  # 用于接口返回的是json的请求,返回json格式信息
            log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (res_data, url))
            return res_data
        except BaseException as e:
            log.info("请求异常！请求失败url: %s,错误信息: %s" % (url, str(e)))

    def post_js(self, url, data=None, params=None, files=None, headers=None, stream=None, timeout=None,
                header_re=False):
        """post请求方法,请求data转为json,结果转成json格式"""
        try:
            data = json.dumps(data)  # 将Python对象编码成JSON字符串
            r = requests.post(url, data=data, params=params, files=files, headers=headers, stream=stream,
                              timeout=timeout)
            r.encoding = 'utf-8'
            if header_re:
                log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (header_re, url))
                return r.headers
            json_r = r.json()
            log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (json_r, url))
            return json_r
        except BaseException as e:
            log.info("请求异常！请求失败url: %s,错误信息: %s" % (url, str(e)))

    def post_headers(self, url, data=None, params=None, files=None, headers=None, stream=None, timeout=None,
                header_re=False):
        """post请求方法,返回headers参数，用于登录前置"""
        try:
            data = json.dumps(data)  # 将Python对象编码成JSON字符串
            r = requests.post(url, data=data, params=params, files=files, headers=headers, stream=stream,
                              timeout=timeout)
            r.encoding = 'utf-8'
            if header_re:
                log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (header_re, url))
                return r.headers
            json_r = r.json()
            log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (json_r, url))
            return json_r
        except BaseException as e:
            log.info("请求异常！请求失败url: %s,错误信息: %s" % (url, str(e)))

    def del_js(self, url, data=None, params=None, files=None, headers=None, stream=None, timeout=None,
               header_re=False):
        """delete请求方法,请求data转为json,结果转成json格式"""
        try:
            data = json.dumps(data)  # 将Python对象编码成JSON字符串
            r = requests.delete(url, data=data, params=params, files=files, headers=headers, stream=stream,
                                timeout=timeout)
            r.encoding = 'utf-8'
            if header_re:
                log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (header_re, url))
                return r.headers
            json_r = r.json()
            log.info("请求成功！Test执行接口返回结果: %s, 请求地址: %s" % (json_r, url))
            return json_r
        except BaseException as e:
            log.info("请求异常！请求失败url: %s,错误信息: %s" % (url, str(e)))
